home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / rend10.lzh / REND1.0 / anim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-01  |  7.0 KB  |  230 lines

  1. /*
  2.  * Title:
  3.  *    anim.c
  4.  *
  5.  * Authors:
  6.  *    Michael P. Schenck
  7.  *
  8.  * Purpose:
  9.  *    This demo is a simple animation with 18 objects composed of 261 verticies (217 polys)
  10.  *      which is run for 1000 frames.  It spins the viewpoint around a scene that has a 100
  11.  *       polygon plane and a ring of 12 cubes rotating above a ground plane and surrounded
  12.  *    by 4 pyramids.  On an A4000/040, this demo completes in 50 sec (avg of 20fps).
  13.  *    It is interesting to note that running 020 code (no fpu pipelining optimizations) on
  14.  *    an 040 produces an avg 12fps.  This just goes to show you how fast the 040 can work
  15.  *    with floating point math!  What the compiler actually does is sequence these instructions
  16.  *    to execute in parallel with other CPU instructions. 
  17.  *       
  18.  * Copyright Info:
  19.  *    Copyright (C) 1993, 1994 -- by Michael P. Schenck, 
  20.  *    (mps4466@ultb.isc.rit.edu)
  21.  *    
  22.  *    This program is free software; you can redistribute it and/or modify
  23.  *    it under the terms of the GNU General Public License as published
  24.  *    by the Free Software Foundation; either version 2 of the License,
  25.  *    or (at your option) any later version.
  26.  *
  27.  *    This software is distributed in the hope that it will be useful, but
  28.  *    WITHOUT ANY WARRANTY; without even the implied warranty of
  29.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30.  *    GNU General Public License for more details.
  31.  *
  32.  *      For a copy of the GNU General Public License
  33.  *    write to the Free Software Foundation, 675 Mass Ave,
  34.  *    Cambridge, MA  02139, USA.
  35.  *
  36.  */
  37.  
  38. #include <stdlib.h>
  39. #include "include/graphicsubsystem.h"
  40.  
  41. void cleanexit(void);
  42.  
  43. UWORD colortable[2] = {0x007,0xFFF};    /* Rgb format (blue and white). */
  44.      
  45. UBYTE i,error,done,type = SUBOBJECT;    
  46. ULONG prim[19];             /* Primitive IDs. */
  47. struct Action *node[19];        /* Pointers to actions blocks in display map. */
  48. struct View *view = NULL;        /* Pointer to view structure. */
  49. MATRIX m[19];                  /* Pointers to matricies. */
  50. FLOAT j,k;
  51.     
  52. main ()
  53. {
  54.     /* Open a 640x400 screen with background blue and lines white and specify
  55.        a model library path. */
  56.  
  57.    if((error = opengraphics(640,400,colortable,"ModelLibrary/")) != SUCCESS)
  58.        return(error);
  59.     
  60.     /* Allocate all the matricies we need. */ 
  61.     
  62.    for(i=0;i<19;i++) {
  63.       if((m[i] = allocatematrix()) == NULL)
  64.          cleanexit();
  65.    }
  66.    
  67.     /* Here we are setting it to an "identity" transformation ... nothing yet. */
  68.     
  69.    setsrttrans(1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,m[0]);
  70.    
  71.        /* Create root action for ring of cubes, no model here. */
  72.    
  73.    if((node[0] = createrootaction(m[0],NOMODEL)) == NULL)
  74.        cleanexit();     
  75.  
  76.     /* Get twelve cubes (uses same model...MULTILINK and set up transformation 
  77.        matrix for each. */
  78.  
  79.    for(i=1;i<13;i++) {
  80.       if((prim[i] = requestmodel("cube.bin",MULTILINK)) == NOMODEL)
  81.         cleanexit();
  82.           
  83.     /* Use i to create ring by setting translation in x to 10.0 units and rotation in z to
  84.        an angle based on i. */
  85.      
  86.       setstrtrans(1.0,1.0,1.0,10.0,0.0,0.0,0.0,0.0,(0.5235*(double)i),m[i]);
  87.        
  88.     /* Insert action after node specified (i-1) and set the transformation.
  89.        NOTE: The first cube set is set as a subobject, then each cube after
  90.        that is on the same level in the display traversal with the subobject. */ 
  91.     
  92.       if((node[i] = createaction(node[i-1],m[i],SET,prim[i],type)) == NULL) 
  93.         cleanexit();
  94.       type = CUROBJECT;    /* Switch to curlevel. */
  95.    }
  96.  
  97.     /* Set up rotating plane. */
  98.  
  99.    if((prim[13] = requestmodel("plane10.bin",SINGLELINK)) == NOMODEL)
  100.         cleanexit();
  101.    setsrttrans(1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,m[13]);
  102.    if((node[13] = createrootaction(m[13],prim[13])) == NULL)
  103.        cleanexit();     
  104.    
  105.        /* Setup small ground plane.. will not move. */
  106.    
  107.    if((prim[14] = requestmodel("plane5.bin",MULTILINK)) == NOMODEL)
  108.         cleanexit();
  109.    setsrttrans(100.0,100.0,100.0,0.0,0.0,0.0,0.0,0.0,-50.0,m[14]);
  110.    if((node[14] = createrootaction(m[14],prim[14])) == NULL)
  111.        cleanexit();     
  112.     
  113.     /* Set up four surrounding pyramids... will not move and all using same model. */
  114.     
  115.    if((prim[15] = requestmodel("pyramid.bin",MULTILINK)) == NOMODEL)
  116.         cleanexit();
  117.    setsrttrans(25.0,25.0,75.0,0.0,0.0,0.0,300.0,0.0,25.0,m[15]);
  118.    if((node[15] = createrootaction(m[15],prim[15])) == NULL)
  119.        cleanexit();     
  120.    
  121.    if((prim[16] = requestmodel("pyramid.bin",MULTILINK)) == NOMODEL)
  122.         cleanexit();
  123.    setsrttrans(25.0,25.0,75.0,0.0,0.0,0.0,-300.0,0.0,25.0,m[16]);
  124.    if((node[16] = createrootaction(m[16],prim[16])) == NULL)
  125.        cleanexit();     
  126.    
  127.    if((prim[17] = requestmodel("pyramid.bin",MULTILINK)) == NOMODEL)
  128.         cleanexit();
  129.    setsrttrans(25.0,25.0,75.0,0.0,0.0,0.0,0.0,300.0,25.0,m[17]);
  130.    if((node[17] = createrootaction(m[17],prim[17])) == NULL)
  131.        cleanexit();     
  132.    
  133.    if((prim[18] = requestmodel("pyramid.bin",MULTILINK)) == NOMODEL)
  134.         cleanexit();
  135.    setsrttrans(25.0,25.0,75.0,0.0,0.0,0.0,0.0,-300.0,25.0,m[18]);
  136.    if((node[18] = createrootaction(m[18],prim[18])) == NULL)
  137.        cleanexit();     
  138.    
  139.        /* Allocate and initialize a view structure. */
  140.    
  141.    view = allocateview();
  142.    view->x = 0.0;        /* X,Y,Z position looking at. */
  143.    view->y = 0.0;    
  144.    view->z = 0.0;
  145.    view->phi = 0.0;        /* Standard spherical coords of camera relative to that pos. */
  146.    view->theta = 2.0;
  147.    view->ro = 300.0;        /* Magnitude of vector depicted by above two values. */
  148.    view->d = 1.0;        /* Position of front clipping plane. */
  149.    view->f = 1000.0;        /* Position of far clipping plane. */
  150.    
  151.                    /* The near and far clipping plane can be based on ro. Try
  152.                    setting them to 150 and 450 respectively and rerun.
  153.                    You will see how they effect the display. */
  154.    
  155.        /* Start animation loop.  Always moving the viewpoint and rotating plane and cubes. */
  156.    
  157.    k=0;
  158.    for(j=0.0;j<500.0;j+=0.5) {
  159.       
  160.     if(j>275) {            
  161.        view->ro += 1.5;
  162.        view->phi += 0.010;
  163.     }
  164.     else
  165.        view->ro -= 0.5;
  166.     if(k>=10)
  167.        view->theta -= 0.05;
  168.      if((k>=15)&&(view->phi < 1.4))
  169.        view->phi += 0.01; 
  170.  
  171.     /* Configuring view computes viewing transformation matrix. */
  172.  
  173.     configureview(view);
  174.  
  175.     k += 0.2;    /* Spin some more. */
  176.     
  177.     /* Update cube ring transformation. */
  178.     
  179.     setsrttrans(4.0,4.0,4.0,k+1,k,k+1,-50.0,0.0,0.0,m[0]);
  180.     
  181.     /* Tell system that a matrix has changed so that it will recompute the portion
  182.        of the display map below that point (update entire circle of cubes...we only
  183.        changed the root transformation to rotate all of them). */
  184.  
  185.     node[0]->changed = TRUE;    
  186.     
  187.     /* Update plane rotation. */
  188.     
  189.     setsrttrans(40.0,40.0,40.0,k,k+1,k,50.0,0.0,0.0,m[13]);
  190.     
  191.     /* Change flag here too. */
  192.     
  193.     node[13]->changed = TRUE;
  194.  
  195.     /* See if a key has been pressed, if so exit. */
  196.  
  197.     if(getinput(NOWAIT))    /* No wait will allow us to continue. */
  198.        cleanexit();
  199.  
  200.     /* Update the screen with all the new changes. */
  201.  
  202.         displaygraphics();
  203.     
  204.     /* Loop around until done or key is pressed. */    
  205.    }
  206.    cleanexit();
  207. }
  208.  
  209. void cleanexit()
  210. {  
  211.        /* Free the matricies. */
  212.     
  213.    for(i=0;i<19;i++)
  214.        if(m[i])
  215.        freematrix(m[i]);
  216.     
  217.     /* Release the view structure. */
  218.     
  219.    if(view)
  220.       releaseview(view);
  221.    
  222.        /* Shutdown */
  223.     
  224.    closegraphics();
  225.    
  226.    exit(0);
  227. }   
  228.    
  229.    
  230.